<?php
//--------------------------------------------------
//	Filename: gym_view.php
//--------------------------------------------------
// Information:
// This file shows the gym page. 
//--------------------------------------------------
if( !defined('IN_INDEX') )
{
	printError("ACCESS DENIED", "This page is hidden.");
	exit;
}

include('inc/functions_gym.php');

$gym_id = @$_REQUEST['gym_id'];
$task	= @$_REQUEST['task'];

function redirect($url)
{
?>
	<script type="text/javascript">
		{
		window.location = "<?php echo $url;?>";
		}
	</script>
<?php 
}
if(!checkSession())
{
			
			redirect("index.php?action=login");
	exit;
}
if(who_am_i()== 'ADMIN' || who_am_i() == 'MOD'){
	 
}else{
	redirect("index.php");
}
?>


<?php 
	// Upload profile picture
if(isset($_POST['task']) AND $_POST['task'] == 'upload_image' )
{
	//Get file attributes
	$name = $_FILES['gym_logo']['name'];
	$size = $_FILES['gym_logo']['size'];
	$tmp_name = $_FILES['gym_logo']['tmp_name'];
	
	if(substr($name, strpos($name, '.') + 1) == "JPG" || substr($name, strpos($name, '.') + 1) == "jpg" || substr($name, strpos($name, '.') + 1) == "jpeg" || substr($name, strpos($name, '.') + 1) == "JPEG")
	{
			
		// Start upload process
		$saving_name = time()."_$name";
		$location = "gym_pic/$saving_name";
		
		// Move picture to folder
		move_uploaded_file($tmp_name, $location);
		
	
		//Update the users avatarpath
		update_gym_logo($saving_name, $gym_id);

		echo"<br /><br /><br /><br /><br /><br /><br /><br /><br />
		<center><meta http-equiv=\"refresh\"content=\"3;url=index.php?action=gym&sub=edit&gym_id=$gym_id\">";
		echo "<br/><h2>" . ucfirst(translate('picture_uploaded')) . "</h2>\n";
		echo "<br/><br />" . translate('redirectedin'). " <a href=\"index.php?action=gym&sub=edit&gym_id=$gym_id\">" . translate('clickhere') . "</a>.</center><div class='fixed'>
					</div>
				</div>
				</div>
			<div class='fixed'>
			</div>
			<div id='footer'>
			<a id='gotop' href='#' onclick='MGJS.goTop();return false;'>" . translate('top') . "</a>
				<div align='center' id='copyright'>&copy; " . translate('copyrightyear') . " " . translate('header_text'). " - " .  translate('curversion') ." - <a href='index.php?action=contact'>" . translate('contact') ."</a> - <a href='index.php?action=about'>" . translate('about') . "</a> - " . translate('onlineusers'). ": " . getActiveUsers() . "<br />
				</div>";
		exit;
		}
		else
		{
		echo"<br /><br /><br /><br /><br /><br /><br /><br /><br />
			<center><meta http-equiv=\"refresh\"content=\"3;url=index.php?action=gym&sub=edit&gym_id=$gym_id\">";
			echo "<br/><h2>" . ucfirst(translate('please_select_picture')) . "</h2>\n";
			echo "<br/><br />" . translate('redirectedin'). " <a href=\"index.php?action=gym&sub=edit&gym_id=$gym_id\">" . translate('clickhere') . "</a>.</center><div class='fixed'>
						</div>
					</div>
					</div>
				<div class='fixed'>
				</div>
				<div id='footer'>
				<a id='gotop' href='#' onclick='MGJS.goTop();return false;'>" . translate('top') . "</a>
					<div align='center' id='copyright'>&copy; " . translate('copyrightyear') . " " . translate('header_text'). " - " .  translate('curversion') ." - <a href='index.php?action=contact'>" . translate('contact') ."</a> - <a href='index.php?action=about'>" . translate('about') . "</a> - " . translate('onlineusers'). ": " . getActiveUsers() . "<br />
					</div>";
			exit;
		}
}

if(isset($_POST['task']) AND $_POST['task'] == 'delete_image'){
	update_gym_logo('', $gym_id);
}

if(who_am_i()== 'ADMIN' || who_am_i() == 'MOD'){
	$disable = "";
}else{
	$disable = " disable='disable'";
}

if(isset($_POST['task']) AND $_POST['task'] == 'save_data'){
	$array_fetch_data = array('gym_name','gym_tlf','gym_address','gym_desc','gym_content','gym_url');
	$array_save = array();
	foreach($array_fetch_data as $key){
		$array_save["$key"] = mysql_real_escape_string($_POST["$key"]);
	}
	
	if(($_POST["gym_activated"] == "") || ($_POST["gym_activated"] == null)){
		$gymTemp = fetch_gym_by_name($array_save['gym_name']);
		if($gymTemp != null) {
			update_trainning_table($gym_id, $gymTemp['gym_id'], $gymTemp['gym_name']);
			delete_gym($gym_id);
			redirect('index.php?action=gym&sub=edit&gym_id='.$gymTemp['gym_id']);
		}
	}
	
	update_gym_data($gym_id,$array_save);
	
	if($disable == ''){
		update_gym_name_at_training_tbl($gym_id,$_POST['gym_name']);
	}
}


$gym_info_array = fetch_gym_by_id($gym_id);
?>

<!-- TinyMCE -->
<script type="text/javascript" src="jscript/tiny_mce/tiny_mce.js"></script>

<script type="text/javascript">
	tinyMCE.init({
        // General options
		language : "<?php if($USER->getLang() == 'no')
		{
			echo "no";
		}
		elseif($USER->getLang() == 'en')
		{
		 echo "en";	
		}
		else
		{
			echo "en";	
		}
		?>",
         mode : "textareas",
        theme : "advanced",
		editor_selector : "mceEditor",
        editor_deselector : "mceNoEditor",

        plugins : "spellchecker,pagebreak,style,layer,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,imagemanager",

        // Theme options
        theme_advanced_buttons1 : "save,newdocument,|,undo,redo,|,cut,copy,paste,pastetext,pasteword,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect|,forecolor,backcolor,|,charmap,emotions,iespell,media,advhr,|,image,insertimage",
		
        theme_advanced_buttons2 : "search,replace,bullist,numlist,|,outdent,indent,blockquote,|,link,unlink,anchor,cleanup,code,|,insertdate,inserttime,preview,|,hr,removeformat,visualaid,|,sub,sup,|,print,|,fullscreen,|,insertlayer,moveforward,movebackward,absolute,|,styleprops,spellchecker,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,blockquote,pagebreak",
		
		theme_advanced_buttons3 : "",
		
        theme_advanced_buttons4 : "",
		
        theme_advanced_toolbar_location : "top",
        theme_advanced_toolbar_align : "left",
        theme_advanced_statusbar_location : "bottom",
        theme_advanced_resizing : true,

        // Skin options
        skin : "o2k7",
        skin_variant : "silver",
	});
</script>
<!-- /TinyMCE -->


<div id="postpath">
	<?php echo translate('edit_gym').": <b>".$gym_info_array['gym_name']."</b>";?>
</div>

<br /><br /><br />

<form name="gym_edit" id="gym_edit" method="POST" action="index.php?action=gym&sub=edit&gym_id=<?php echo $gym_id;?>" enctype="multipart/form-data" >
	<div>
		<div style="float:left;width:50%;">
		    <table>
				<tr>
					<td><?php echo translate('gym_name');?></td>
					<td><input type="text" <?php echo $disable;?> name="gym_name" id="gym_name" size="50" maxlength="255" value="<?php echo $gym_info_array['gym_name'];?>" /></td>
				</tr>
				<tr>
					<td><?php echo translate('gym_tlf');?></td>
					<td><input type="text" name="gym_tlf" id="gym_tlf" size="50" maxlength="11" value="<?php echo $gym_info_array['gym_tlf'];?>" /></td>
				</tr>
				<tr>
					<td><?php echo translate('gym_url');?></td>
					<td><input type="text" name="gym_url" id="gym_url" size="50" maxlength="250" value="<?php echo $gym_info_array['gym_url'];?>" /></td>
				</tr>
				<tr>
					<td><?php echo translate('gym_address');?></td>
					<td><input type="text" name="gym_address" id="gym_address" size="50" maxlength="250" value="<?php echo $gym_info_array['gym_address'];?>" /></td>
				</tr>
				<tr>
					<td><?php echo translate('gym_desc');?></td>
					<td><textarea cols="50" rows="5" name="gym_desc" id="gym_desc" maxlength="500" class="mceNoEditor"><?php echo $gym_info_array['gym_desc'];?></textarea></td>
				</tr>
			</table>
		</div>
		
		<div style="float:left;width:50%;">
			 
				<table>
					<tr>
						<td colspan="2">
							<h1><?php echo translate('gym_logo');?></h1>
							<br />
							<?php if($gym_info_array['gym_logo']):?>
								<img src="<?php echo gym_banner($gym_id,150,100);?>" />
							<?php endif;?>
						</td>
					</tr>
					<tr>
						<td><input type="file" name="gym_logo" id="gym_logo" /></td>
						<td><input type="button" value="<?php echo ucfirst(translate('uploadpic')); ?>" name="submit_form_image" id="submit_form_image" onclick="submit_image();" /></td>
					</tr>
					<tr>
						<td colspan="2" align="center">
							<input type="button" value="<?php echo ucfirst(translate('remove_picture')); ?>" name="delete_image_submit" id="delete_image_submit" onclick="delete_images();"/>
						</td>
					</tr>
					<input type="hidden" id="gym_id" name="gym_id" value="<?php echo $gym_id; ?>" />
					<input type="hidden" id="task" name="task" value="" />
				</table>
			 
		</div>
		
		<div class="fixed"> </div>
		<div>
			 <table>
			 	<tr><td><?php echo translate('gym_content');?></td></tr>
				<tr>
					<td width="100%"><textarea name="gym_content" id="gym_content" class="mceEditor" style="heigh:300px;"><?php echo $gym_info_array['gym_content'];?></textarea></td>
				</tr>
				<tr>
					<td>
						<input type="hidden" name="gym_activated" value="<?php echo $gym_info_array['gym_activated'] ?>" />
						<input type="button" value=" <?php echo ucfirst(translate('save')); ?> " onclick="save_data();"/>
						&nbsp;&nbsp;&nbsp;
						<input type="button" value=" <?php echo ucfirst(translate('close')); ?> " onclick="close_form();"/>
					</td>
					 
				</tr>
			</table>
		</div>
		 
	</div>
</form>

<script type="text/javascript">
<!--
	function submit_image(){
		jQuery('#task').val('upload_image');
		jQuery('#gym_edit').submit();
	}
	
	function delete_images(){
		jQuery('#task').val('delete_image');
		jQuery('#gym_edit').submit();
	}
	
	function save_data(){
		jQuery('#task').val('save_data');
		jQuery('#gym_edit').submit();
	}
	
	function close_form(){
		window.location = "index.php?action=gym&sub=view&gym_id=<?php echo $gym_id;?>";
	}
//-->
</script>
